/**
 * Title :	jFamilyTree generator
 * Description: jFamilyTree generator
 * Version:	provider
 * Copyright (c) 2001-2008
 * Terms of use:see license agreement at http://treebolic.sourceforge.net/en/license.htm
 * Author: Bernard Bou
 * Company: bsys
 */
package jFamilyTree.dialogs.main;

import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.util.Properties;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.UIManager;

import jFamilyTree.Messages;
import jFamilyTree.commons.FileDialogs;
import jFamilyTree.commons.Persist;
import jFamilyTree.commons.SettingsDialog;

/**
 * Settings dialog
 * 
 * @author Bernard Bou
 */
public class XSettingsDialog extends SettingsDialog
{
	private static final long serialVersionUID = 1L;

	/**
	 * Repository text
	 */
	private JTextField theRepositoryTextField;

	/**
	 * Constructor
	 * @param theseProperties
	 */
	public XSettingsDialog(Properties theseProperties)
	{
		super(theseProperties);
	}

	/* (non-Javadoc)
	 * @see jFamilyTree.commons.SettingsDialog#initialize()
	 */
	@Override
	protected void initialize()
	{
		super.initialize();
		theRepositoryTextField = new JTextField(32);
		theRepositoryTextField.setToolTipText(Messages.getString("XSettingsDialog.2")); //$NON-NLS-1$
		JLabel theRepositoryFolderLabel = new JLabel(Messages.getString("XSettingsDialog.1")); //$NON-NLS-1$
		JButton thisRepositoryBrowseButton = new JButton(Messages.getString("XSettingsDialog.3")); //$NON-NLS-1$
		thisRepositoryBrowseButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(java.awt.event.ActionEvent event)
			{
				String thisFolder = FileDialogs.getFolder(theSettings.getProperty("base",".")); //$NON-NLS-1$ //$NON-NLS-2$
				if (thisFolder != null && !thisFolder.isEmpty())
					theRepositoryTextField.setText(thisFolder);
				else
					theRepositoryTextField.setText(Messages.getString("XSettingsDialog.10")); //$NON-NLS-1$
			}
		});
		
		theDataPanel.add(theRepositoryFolderLabel, new GridBagConstraints(0, 2, 1, 1, 0., 0., GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 20, 0, 10), 0, 0));
		theDataPanel.add(theRepositoryTextField, new GridBagConstraints(1, 2, 1, 1, 1., 0., GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 0, 10), 0, 0));
		theDataPanel.add(thisRepositoryBrowseButton, new GridBagConstraints(2, 2, 1, 1, 0., 0., GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 10, 0, 20), 0, 0));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.Component#setVisible(boolean)
	 */
	public void setVisible(boolean thisFlag)
	{
		if (thisFlag)
		{
			// read properties into components
			theRepositoryTextField.setText(theSettings.getProperty(Messages.getString("XSettingsDialog.11"), Messages.getString("XSettingsDialog.10"))); //$NON-NLS-1$ //$NON-NLS-2$
		}
		else
		{
			if (ok)
			{
				// update properties from components
				String thisRepository = theRepositoryTextField.getText();
				if (thisRepository == null || thisRepository.isEmpty() || thisRepository.equals(Messages.getString("XSettingsDialog.10"))) //$NON-NLS-1$
					theSettings.remove(Messages.getString("XSettingsDialog.11")); //$NON-NLS-1$
				else
					theSettings.setProperty(Messages.getString("XSettingsDialog.11"), thisRepository); //$NON-NLS-1$
			}
		}
		super.setVisible(thisFlag);
	}

	static public void main(String[] theseArgs)
	{
		UIManager.put("swing.boldMetal", false); //$NON-NLS-1$
		Properties theseSettings = Persist.getSettings("jFamilyTree-generator"); //$NON-NLS-1$
		XSettingsDialog thisDialog = new XSettingsDialog(theseSettings);
		thisDialog.setModal(true);
		thisDialog.setVisible(true);
		thisDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		if (thisDialog.ok)
			Persist.saveSettings("jFamilyTree-generator", theseSettings); //$NON-NLS-1$
		System.exit(0);
	}
}
